<template>
    <div class="add">
        <el-header class="title" style="position: relative;top: 20px;font-size: 30px">
            <i class="el-icon-s-order">基本信息</i>
        </el-header>
        <el-divider></el-divider>
        <el-form :model="law" label-width="100px" label-position="top" style="margin-left: 20px">
            <div class="require">
                <el-container>
                    <el-form-item label="服务时间：">
                        <el-date-picker
                                style="width: 500px;"
                                v-model="law.collectionTime"
                                type="date"
                                value-format="yyyy-MM-dd"
                                placeholder="选择日期">
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item label="服务费用：" style="margin-left: 20px">
                        <el-input type="number" v-model="law.caseAgencyfee" style="width: 500px;" :readonly="status=='update'"></el-input>
                    </el-form-item>
                </el-container>

                <el-container>
                    <el-form-item label="咨询/代书事项：">
                        <el-input v-model="law.caseCause" style="width: 500px;"></el-input>
                    </el-form-item>
                    <el-form-item label="委托人：" style="margin-left: 20px">
                        <el-input v-model="law.caseWtr" style="width: 500px;"></el-input>
                    </el-form-item>
                </el-container>

                <el-container>
                    <el-form-item label="服务类型：">
                        <el-select v-model="law.caseCaseTypeId" :value="law.caseCaseTypeId" style="width: 500px;">
                            <el-option label="口头法律咨询(人次)" value="101101"></el-option>
                            <el-option label="书面法律咨询(人次)" value="101102"></el-option>
                            <el-option label="代写法律文书(件)" value="101103"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="诉讼标的：" style="margin-left: 20px">
                        <el-input readonly v-model="law.caseLawsuitobj" style="width: 500px;"></el-input>
                    </el-form-item>
                </el-container>
            </div>

            <div class="other-container">
                <el-header class="title" style="position: relative;top: 20px;left:-20px;font-size: 30px">
                    <i class="el-icon-s-order">其他选填</i>
                    <el-button size="small" @click="handleToggleOther" class="toogle">{{other.text}}</el-button>
                </el-header>

                <div class="other" v-if="other.enable">
                    <el-container>
                        <el-form-item label="提供服务人次：">
                            <el-input type="number" v-model="law.standard" style="width: 500px;"></el-input>
                        </el-form-item>
                        <el-form-item label="对方当事人：" style="margin-left: 20px">
                            <el-input v-model="law.caseOppositeParties" style="width: 500px;"></el-input>
                        </el-form-item>
                    </el-container>

                    <el-container>
                        <el-form-item label="备注：">
                            <el-input type="textarea" v-model="law.caseRemarks" style="width: 1000px;"></el-input>
                        </el-form-item>
                    </el-container>

                    <el-container>
                        <el-form-item label="相关文件：">
                            <el-upload
                                    name="multipartfile"
                                    action="http://localhost/upload"
                                    :headers="{token:this.$store.state.token}"
                                    :data="{folder:'law'}"
                                    class="upload-demo"
                                    ref="upload"
                                    :limit="1"
                                    :multiple="false"
                                    :auto-upload="false"
                                    :on-success="handleUploadSuccess">
                                <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
                                <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上传到服务器</el-button>
                            </el-upload>
                        </el-form-item>
                        <el-input v-model="law.caseCorrelationFile" v-show="false"></el-input>
                    </el-container>
                </div>
                <el-divider></el-divider>

                <el-container class="decrease-add-container">
                    <el-header class="title" style="position: relative;top: 20px;left:-20px;font-size: 30px">
                        <i class="el-icon-user-solid">办案律师</i>
                        <el-button size="small" @click="handleAddRow" class="row-add">增加一行</el-button>
                    </el-header>
                    <el-divider></el-divider>

                    <el-container v-for="(legal,index) in cwPerformancesplitList" :key="index" :class="{'legal-box':index!=0}">
                        <!--身份-->
                        <el-select v-model="legal.caseAttorney" :value="legal.caseAttorney" :disabled="index==0" style="margin-left: 100px">
                            <el-option value="主办律师" v-if="index==0"></el-option>
                            <el-option value="督办律师"></el-option>
                            <el-option value="协助律师"></el-option>
                        </el-select>
                        <!--名字-->
                        <el-select @change="handleChangeUser(index,legal)"  v-model="legal.hrEmpId" :value="legal.empName" style="margin-left: 30px">
                            <el-option
                                    :key="emp.id"
                                    v-for="emp in empList"
                                    :value="emp.id"
                                    :label="emp.empName"
                            ></el-option>
                        </el-select>
                        <!--金额-->
                        <el-input type="number" v-model="legal.cwPerformancesplit" style="width: 200px;margin-left: 30px"></el-input>
                        <!--类型-->
                        <el-select v-model="legal.cwSplitType" :value="legal.caseAttorney" style="margin-left: 30px">
                            <el-option label="固定金额" value="固定金额"></el-option>
                            <el-option label="百分比" value="百分比"></el-option>
                        </el-select>
                        <el-button @click="handleRemoveRow(index)" v-if="index!=0" type="danger" style="margin-left: 10px">删除一行</el-button>
                    </el-container>
                </el-container>

                <el-container style="margin-top: 20px;margin-left: 100px">
                    <el-form-item>
                        <el-button type="primary" @click="handleSubmitForm">确认提交</el-button>
                        <el-button @click="handleBack">返回</el-button>
                    </el-form-item>
                </el-container>
            </div>
        </el-form>
    </div>
</template>

<script>
    import HrEmpService from '../../../model/hyc/HrEmpService.js'
    const hrEmpService = HrEmpService.getInstance()

    export default {
        props:['law','status','cwPerformancesplitList'],
        data() {
            return {
                other:{
                    text:'展开',
                    enable:false
                },
                empList:[]
            }
        },
        methods:{
            //提交表单
            handleSubmitForm(){
                if(this.status == 'add'){
                    this.$emit('add')
                }else{
                    this.$emit('update')
                }
            },
            //当律师用户下拉框改变时执行的方法
            handleChangeUser(index,legal){
                this.empList.forEach(item => {
                    if(item.id == legal.hrEmpId){
                        legal.empName = item.empName
                    }
                })
            },
            //拿到员工列表的方法
            getEmpList(){
                hrEmpService.getEmpsByRole('律师,律师长').then(response => {
                    if(response.data.code === 0){
                        this.empList = response.data.data
                    }
                })
            },
            handleAddRow(){
                this.cwPerformancesplitList.push({
                    hrEmpId:'',
                    cwSplitType:'百分比',
                    caseAttorney:'督办律师',
                    cwPerformancesplit:0,
                    cwRealSplitMoney:0,
                    type:1
                })
            },
            handleRemoveRow(index){
                this.cwPerformancesplitList.splice(index,1)
            },
            //切换其他选项是否显示
            handleToggleOther(){
                this.other.enable = !this.other.enable
                if(this.other.enable){
                    this.other.text = '收起'
                }else{
                    this.other.text = '展开'
                }
            },
            handleBack(){
                this.$emit('back')
            },
            //文件上传成功回调
            handleUploadSuccess(response){
                this.law.caseCorrelationFile = response.data
            },
            submitUpload(){
                this.$refs.upload.submit();
            }
        },
        created(){
            this.getEmpList()
        }
    }
</script>

<style scoped>
    .add{
        width: 100%;
    }
    .require{
        width: 100%;
    }
    .other-container{
        width: 100%;
    }
    .toogle,.row-add{
        position: relative;
        top: -5px;
        margin-left: 30px;
    }
    .legal-box{
        margin-top: 10px;
    }
</style>
